package psd.model.Prolog.Converters;

import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.dom.ArrayCreation;
import org.eclipse.jdt.core.dom.Expression;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologArrayCreationConverter extends PrologConverter implements
IConverter<ArrayCreation>{

	public PrologArrayCreationConverter(FactGenerationToolbox toolbox) {
		super(toolbox);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean convert(ArrayCreation node) {
		 if(node.getInitializer() != null)
	        {
			  generationToolBox.getIDResolver().setEquivalence(node, node.getInitializer());
	        }
	        StringBuffer dimbuffer = new StringBuffer("[");
	        List dims = node.dimensions();
	        boolean implicitDimensions = true;
	        boolean first = true;
	        Expression e;
	        for(Iterator i = dims.iterator(); i.hasNext(); dimbuffer.append( generationToolBox.getIDResolver().getID(e)))
	        {
	            implicitDimensions = false;
	            if(!first)
	            {
	                dimbuffer.append(", ");
	            } else
	            {
	                first = false;
	            }
	            e = (Expression)i.next();
	        }

	        dimbuffer.append("]");
	        String dms = dimbuffer.toString();
	        if(!implicitDimensions)
	        {
	            String elems = PrologCommon.getEmptyList();
	            String args[] = {
	                dms, elems,  generationToolBox.getTypeResolver().getTypeTerm(node.resolveTypeBinding())
	            };
	            createBodyFact(node, "newArrayT", args);
	        }
	        return true;
	}

}
